#! /usr/bin/python3

import os
import argparse
import numpy as np

no_ebpf_sys_cpu_used = []
no_ebpf_sys_mem_used = []
ebpf_sys_cpu_used = []
ebpf_sys_mem_used = []
ebpf_profile_cpu_used = []
ebpf_profile_mem_used = []

def handle_cpu(file_path, is_ebpf):

    if not os.path.isfile(file_path):
        return
    
    search_sys_cpu= "%Cpu(s)"
    search_sys_mem= "cache"
    search_profile_process = "profile"


    with open(file_path, 'r', encoding='utf-8') as file:
        
        for line in file:
            
            if  search_sys_cpu in line:
                idel_cpu = str.strip(line.split(':')[1])
                idel_cpu = str.strip(idel_cpu.split(',')[3])
                idel_cpu = float(str.strip(idel_cpu[:-2]))
                used_cpu = round(100.0 - idel_cpu,2)

                if (is_ebpf == True):
                    ebpf_sys_cpu_used.append(used_cpu)
                else :
                    no_ebpf_sys_cpu_used.append(used_cpu)

            elif  search_sys_mem in line:       
                used_mem = str.strip(line.split(':')[1])
                used_mem = str.strip(used_mem.split(',')[2])
                used_mem = float(str.strip(used_mem[:-5]))

                if (is_ebpf == True):
                    ebpf_sys_mem_used.append(used_mem)
                else :
                    no_ebpf_sys_mem_used.append(used_mem)
                
            elif search_profile_process in line:
                # s_position = line.find('S')
                profile_cpu_used = float(str.strip(line[48:54]))
                profile_cpu_used = round(profile_cpu_used, 2)
                ebpf_profile_cpu_used.append(profile_cpu_used)
                #
                profile_mem_used = float(str.strip(line[56:62]))
                profile_mem_used = round(profile_mem_used, 2)
                ebpf_profile_mem_used.append(profile_mem_used)


    print("{} has process".format(file_path))


if __name__ == '__main__':

    parser = argparse.ArgumentParser(
    description="Get address from libso",
    formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument("--dir", type = str,required = True,
        help="profile process with this PID only")
    args = parser.parse_args()

    current_path = os.getcwd()
    
    print(current_path)

    g =  os.walk("{}".format(args.dir))

    # split file output folded
    for root, dirs, files in g:
        for file in files:

            search_string1 = "cpu_top"
            search_string2 = "ebpf"
            search_string3 = "no"

            if  not search_string1 in file:
                continue

            file_path = os.path.join(root, file)
            if search_string2 in file:
                res = handle_cpu(file_path, True)
                #print("filename:{}; average:{}; p99:{}".format(file,res[0],res[1]))
            elif search_string3 in file:
                res = handle_cpu(file_path, False)
    
    if len(no_ebpf_sys_cpu_used) != 0:
        sys_p99 = np.percentile(no_ebpf_sys_cpu_used, 99)
        sys_p99 = round(sys_p99, 2)
        print("[no-ebpf-cpu] sys_average_cpu:{}, sys_cpu_p99:{}".format(round(sum(no_ebpf_sys_cpu_used)/len(no_ebpf_sys_cpu_used), 2), sys_p99))


    if len(ebpf_sys_cpu_used) != 0 :
        sys_p99 = np.percentile(ebpf_sys_cpu_used, 99)
        sys_p99 = round(sys_p99, 2)
        print("[ebpf-cpu] sys_average_cpu:{}, sys_cpu_p99:{}".format(round(sum(ebpf_sys_cpu_used)/len(ebpf_sys_cpu_used), 2), sys_p99))

    if len(ebpf_profile_cpu_used) != 0:
        profile_p99 = np.percentile(ebpf_profile_cpu_used, 99)
        profile_p99 = round(profile_p99, 2)
        print("[ebpf-cpu] ebpf_average_cpu:{}, ebpf_cpu_p99:{}".format(round(sum(ebpf_profile_cpu_used)/len(ebpf_profile_cpu_used), 2), profile_p99))

    if len(no_ebpf_sys_mem_used) != 0:
        profile_p99 = np.percentile(no_ebpf_sys_mem_used, 99)
        profile_p99 = round(profile_p99, 2)
        print("[no-ebpf-mem] sys_average_mem:{}, sys_mem_p99:{}".format(round(sum(no_ebpf_sys_mem_used)/len(no_ebpf_sys_mem_used), 2), profile_p99))


    if len(ebpf_sys_mem_used) != 0 :
        sys_p99 = np.percentile(ebpf_sys_mem_used, 99)
        sys_p99 = round(sys_p99, 2)
        avg = round(sum(ebpf_sys_mem_used)/len(ebpf_sys_mem_used), 2)
        print("[ebpf-mem] sys_average_mem:{}, sys_mem_p99:{}".format(avg, sys_p99))


    if len(ebpf_profile_mem_used) != 0 :
        sys_p99 = np.percentile(ebpf_profile_mem_used, 99)
        print(sys_p99)
        sys_p99 = round(11*sys_p99*0.01*1024,2)
        avg = sum(ebpf_profile_mem_used)/len(ebpf_profile_mem_used)
        print(min(ebpf_profile_mem_used))
        avg = round(11*avg*0.01*1024,2)
        print("[ebpf-mem] ebpf_average_mem:{}, ebpf_mem_p99:{}".format(avg, sys_p99))